package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ctg.itrdc.platform.common.utils.bean.BeanUtils;
import com.ffcs.crmd.tsp.admin.entity.JobDetail;
import com.ffcs.crmd.tsp.admin.entity.JobDetailRetry;
import com.ffcs.crmd.tsp.admin.entity.JobDetailRunning;
import com.ffcs.crmd.tsp.admin.entity.JobDetailThreadInfo;
import com.ffcs.crmd.tsp.admin.entity.ThreadInfo;
import com.ffcs.crmd.tsp.admin.service.IJobDetailService;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetail;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailParam;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailRetry;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailRunning;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailThreadInfo;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;

/**
 * 
 * 功能说明:子任务服务
 *
 * @author FFCS-ZHONGFUHUA
 * 
 * @since 
 *
 */
@Service
public class JobDetailServiceImpl extends AbstractService implements IJobDetailService {
    
    @Override
    public void addJobDetail(JobDetail jobDetail) throws TspException {
        try {
            TspJobDetail tspJobDetailVO = new TspJobDetail();
            BeanUtils.applyIf(tspJobDetailVO, jobDetail, false);
            tspJobDetailVO.setParams(BeanUtils.copyList(jobDetail.getParams(), TspJobDetailParam.class));
            
            TspClient tspClient = this.getClient(jobDetail.getClusterName());
            TspResult<?> tspResult = tspClient.createJobDetail(tspJobDetailVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void addJobDetail(List<JobDetail> jobDetailList) throws TspException {
        for(JobDetail jobDetail : jobDetailList){
            addJobDetail(jobDetail);
        }
    }
    
    @Override
    public void deleteJobDetail(JobDetail jobDetail) throws TspException {
        try {
            TspJobDetail tspJobDetailVO = new TspJobDetail();
            tspJobDetailVO.setClusterName(jobDetail.getClusterName());
            tspJobDetailVO.setId(jobDetail.getId());
            
            TspClient tspClient = this.getClient(jobDetail.getClusterName());
            TspResult<?> tspResult = tspClient.deleteJobDetail(tspJobDetailVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void deleteJobDetail(List<JobDetail> jobDetailList) throws TspException {
        for(JobDetail jobDetail : jobDetailList){
            deleteJobDetail(jobDetail);
        }
    }
    
    @Override
    public void finish(JobDetailRunning jobDetailRunning) throws TspException {
        try {
            TspJobDetailRunning tspJobDetailRunningVO = new TspJobDetailRunning();
            BeanUtils.applyIf(tspJobDetailRunningVO, jobDetailRunning, false);
            
            TspClient tspClient = this.getClient(jobDetailRunning.getClusterName());
            TspResult<?> tspResult = tspClient.deleteJobDetailRunning(tspJobDetailRunningVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void finish(List<JobDetailRunning> jobDetailRunningList) throws TspException {
        for (JobDetailRunning jobDetailRunning : jobDetailRunningList) {
            finish(jobDetailRunning);
        }
    }
    
    
    @Override
    public JobDetail get(JobDetail jobDetail) throws TspException {
        try {
            TspJobDetail tspJobDetailVO = new TspJobDetail();
            BeanUtils.applyIf(tspJobDetailVO, jobDetail, false);
            
            TspClient tspClient = this.getClient(jobDetail.getClusterName());
            TspResult<TspJobDetail> tspResult = tspClient.queryJobDetail(tspJobDetailVO);
            validateResult(tspResult);
            
            JobDetail rjobDetail = new JobDetail();
            BeanUtils.applyIf(rjobDetail, tspResult.getRetVo().getObject(), false);
            return rjobDetail;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void interrupt(JobDetailRunning jobDetailRunning) throws TspException {
        try {
            TspJobDetailRunning tspJobDetailRunningVO = new TspJobDetailRunning();
            BeanUtils.applyIf(tspJobDetailRunningVO, jobDetailRunning, false);
            
            TspClient tspClient = this.getClient(jobDetailRunning.getClusterName());
            TspResult<?> tspResult = tspClient.interruptJob(tspJobDetailRunningVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void interrupt(List<JobDetailRunning> jobDetailRunningList) throws TspException {
        for (JobDetailRunning jobDetailRunning : jobDetailRunningList) {
            interrupt(jobDetailRunning);
        }
    }
    
    @Override
    public void pasueJobDetail(JobDetail jobDetail) throws TspException {
        try {
            TspJobDetail tspJobDetailVO = new TspJobDetail();
            BeanUtils.applyIf(tspJobDetailVO, jobDetail, false);
            
            TspClient tspClient = this.getClient(jobDetail.getClusterName());
            TspResult<?> tspResult = tspClient.pauseJobDetail(tspJobDetailVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void pasueJobDetail(List<JobDetail> jobDetailList) throws TspException {
        for (JobDetail jobDetail : jobDetailList) {
            pasueJobDetail(jobDetail);
        }
    }
    
    
    @Override
    public List<JobDetail> query(JobDetail jobDetail) throws TspException {
        try {
            TspJobDetail tspJobDetailVO = new TspJobDetail();
            BeanUtils.applyIf(tspJobDetailVO, jobDetail, false);
            
            TspClient tspClient = this.getClient(jobDetail.getClusterName());
            TspResult<TspJobDetail> tspResult = tspClient.queryJobDetail(tspJobDetailVO);
            validateResult(tspResult);
            
            List<TspJobDetail> tspJobDetailList = new ArrayList<TspJobDetail>();
            tspJobDetailList.addAll(tspResult.getRetVo().getDataList());
            return BeanUtils.copyList(tspJobDetailList, JobDetail.class);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public PageInfo<JobDetail> queryPage(JobDetail jobDetail, int pageNumber, int pageSize) throws TspException {
        try {
            TspJobDetail tspJobDetailVO = new TspJobDetail();
            BeanUtils.applyIf(tspJobDetailVO, jobDetail, false);
            tspJobDetailVO.setPageNumber(pageNumber);
            tspJobDetailVO.setPageSize(pageSize);
            
            TspClient tspClient = this.getClient(jobDetail.getClusterName());
            TspResult<TspJobDetail> tspResult = tspClient.queryJobDetail(tspJobDetailVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspJobDetail> tspJobDetailPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<JobDetail> jobDetailPageInfo = new PageInfo<JobDetail>();
            BeanUtils.applyIf(jobDetailPageInfo, tspJobDetailPageInfo, false);
            jobDetailPageInfo.setList(BeanUtils.copyList(tspJobDetailPageInfo.getList(), JobDetail.class));
            return jobDetailPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public PageInfo<JobDetailRetry> queryRetryPage(JobDetailRetry jobDetailRetry, int pageNumber, int pageSize) throws TspException {
        try {
            TspJobDetailRetry tspJobDetailRetryVO = new TspJobDetailRetry();
            BeanUtils.applyIf(tspJobDetailRetryVO, jobDetailRetry, false);
            tspJobDetailRetryVO.setPageNumber(pageNumber);
            tspJobDetailRetryVO.setPageSize(pageSize);
            
            TspClient tspClient = this.getClient(jobDetailRetry.getClusterName());
            TspResult<TspJobDetailRetry> tspResult = tspClient.queryJobDetailRetry(tspJobDetailRetryVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspJobDetailRetry> tspRetryPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<JobDetailRetry> waitingPageInfo = new PageInfo<JobDetailRetry>();
            BeanUtils.applyIf(waitingPageInfo, tspResult.getRetVo().getPageInfo());
            waitingPageInfo.setList(BeanUtils.copyList(tspRetryPageInfo.getList(), JobDetailRetry.class));
            return waitingPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public PageInfo<JobDetailRunning> queryRunningPage(JobDetailRunning jobDetailRunning, int pageNumber, int pageSize) throws TspException {
        try {
            TspJobDetailRunning tspJobDetailRunningVO = new TspJobDetailRunning();
            BeanUtils.applyIf(tspJobDetailRunningVO, jobDetailRunning, false);
            tspJobDetailRunningVO.setPageNumber(pageNumber);
            tspJobDetailRunningVO.setPageSize(pageSize);
            
            TspClient tspClient = this.getClient(jobDetailRunning.getClusterName());
            TspResult<TspJobDetailRunning> tspResult = tspClient.queryJobDetailRunning(tspJobDetailRunningVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspJobDetailRunning> tspRunningPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<JobDetailRunning> runningPageInfo = new PageInfo<JobDetailRunning>();
            BeanUtils.applyIf(runningPageInfo, tspResult.getRetVo().getPageInfo());
            runningPageInfo.setList(BeanUtils.copyList(tspRunningPageInfo.getList(), JobDetailRunning.class));
            return runningPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public JobDetailThreadInfo queryThreadInfo(JobDetailRunning jobDetailRunning) throws TspException {
        try {
            TspJobDetailRunning tspJobDetailRunningVO = new TspJobDetailRunning();
            BeanUtils.applyIf(tspJobDetailRunningVO, jobDetailRunning, false);
            
            TspClient tspClient = this.getClient(jobDetailRunning.getClusterName());
            TspResult<TspJobDetailThreadInfo> tspResult = tspClient.queryJobDetailThreadInfo(tspJobDetailRunningVO);
            validateResult(tspResult);
            
            TspJobDetailThreadInfo tspJobDetailThreadInfoVO = (TspJobDetailThreadInfo) tspResult.getRetVo().getObject();
            JobDetailThreadInfo jobDetailThreadInfo = new JobDetailThreadInfo();
            if (tspJobDetailThreadInfoVO.getJobProcessThread() != null) {
                BeanUtils.applyIf(jobDetailThreadInfo.getJobProcessThread(), tspJobDetailThreadInfoVO.getJobProcessThread(), false);
            }
            
            if (CollectionUtils.isNotEmpty(tspJobDetailThreadInfoVO.getJobProcessBusinessThread())) {
                List<ThreadInfo> jobProcessBusinessThread = BeanUtils.copyList(tspJobDetailThreadInfoVO.getJobProcessBusinessThread(),
                    ThreadInfo.class);
                jobDetailThreadInfo.setJobProcessBusinessThread(jobProcessBusinessThread);
            }
            return jobDetailThreadInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void reset(JobDetailRunning jobDetailRunning) throws TspException {
        try {
            TspJobDetailRunning tspJobDetailRunningVO = new TspJobDetailRunning();
            BeanUtils.applyIf(tspJobDetailRunningVO, jobDetailRunning, false);
            
            TspClient tspClient = this.getClient(jobDetailRunning.getClusterName());
            TspResult<?> tspResult = tspClient.resetJobDetailRunning(tspJobDetailRunningVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void reset(List<JobDetailRunning> jobDetailRunningList) throws TspException {
        for(JobDetailRunning jobDetailRunning : jobDetailRunningList){
            reset(jobDetailRunning);
        }
    }
    
    
    @Override
    public void resumeJobDetail(JobDetail jobDetail) throws TspException {
        try {
            TspJobDetail tspJobDetailVO = new TspJobDetail();
            BeanUtils.applyIf(tspJobDetailVO, jobDetail, false);
            
            TspClient tspClient = this.getClient(jobDetail.getClusterName());
            TspResult<?> tspResult = tspClient.resumeJobDetail(tspJobDetailVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void resumeJobDetail(List<JobDetail> jobDetailList) throws TspException {
        for (JobDetail jobDetail : jobDetailList) {
            resumeJobDetail(jobDetail);
        }
    }
    
}
